package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.testEqu.TestedEqu;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customerSample.mapper.TestedEquMapper;
import com.ruoyi.customerSample.service.ITestedEquService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 受检设备信息Service业务层处理
 * 
 * @author luguanqing
 * @date 2020-11-13
 */
@Service
public class TestedEquServiceImpl implements ITestedEquService
{
    @Autowired(required = false)
    private TestedEquMapper reportEquMapper;


    /**
     *
     * @param reportEqu 受检设备信息
     * @return
     */
    @Override
    public List<TestedEqu> selectReportEquList(TestedEqu reportEqu) {
        return reportEquMapper.selectReportEquList(reportEqu);
    }



    /**
     *
     * @param reportEqu 受检设备大类列表
     * @return
     */
    @Override
    public List<TestedEqu> buildReportEquTree(List<TestedEqu> reportEqu) {
        List<TestedEqu> returnList = new ArrayList<TestedEqu>();
        for (Iterator<TestedEqu> iterator = reportEqu.iterator(); iterator.hasNext();)
        {
            TestedEqu t = (TestedEqu) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.equalsIgnoreCase("0", t.getPid()))
            {
                recursionFn(reportEqu, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = reportEqu;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildReportEquTreeSelect(List<TestedEqu> reportEqu) {
        List<TestedEqu> reportEquTrees = buildReportEquTree(reportEqu);
        return reportEquTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     *
     * @param id 受检单位设备信息ID
     * @return
     */
    @Override
    public TestedEqu selectReportEquById(String id) {
        return reportEquMapper.selectReportEquById(id);
    }

    /**
     * 根据数组id获取名称
     * @param ids
     * @return
     */
    @Override
    public List<String> selectReportEquByIds(String[] ids) {
        return reportEquMapper.selectReportEquByIds(ids);
    }

    /**
     *
     * @param reportEqu 受检单位设备信息
     * @return
     */
    @Override
    public int insertReportEqu(TestedEqu reportEqu) {
        return reportEquMapper.insertReportEqu(reportEqu);
    }

    /**
     * 更新受检单位设备菜单大类信息
     *
     * @param reportEqu 受检单位设备信息
     */
    @Override
    public int updateReportEqu(TestedEqu reportEqu) {
        return reportEquMapper.updateReportEqu(reportEqu);
    }

    /**
     *
     * @param id 受检设备Id
     * @return
     */
    @Override
    public int deleteReportEquById(String id) {
        return reportEquMapper.deleteReportEquById(id);
    }


    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<TestedEqu> list, TestedEqu t)
    {
        // 得到子节点列表
        List<TestedEqu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TestedEqu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<TestedEqu> it = childList.iterator();
                while (it.hasNext())
                {
                    TestedEqu n = (TestedEqu) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<TestedEqu> getChildList(List<TestedEqu> list, TestedEqu t)
    {
        List<TestedEqu> tlist = new ArrayList<TestedEqu>();
        Iterator<TestedEqu> it = list.iterator();
        while (it.hasNext())
        {
            TestedEqu n = (TestedEqu) it.next();
            if (StringUtils.equalsIgnoreCase(n.getPid(), t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TestedEqu> list, TestedEqu t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
